<?php
namespace src\logic;
use \classlib\core as C;
class lrtmpuri {
	const push_uri  = 'rtmp://%s/push_%s?ptoken=%s%08d?vhost=vpush/%d_%d_001';
	const live_uri  = 'rtmp://%s/livepkgr?rtoken=%s%08d/%d_%d_001';
	const conn_uri  = 'rtmp://%s/livepkgr/%d_%d_001';

	/**
	 * 获取拉流地址信息
	 * @param unknown $tocken
	 * @param unknown $app
	 * @param unknown $studioid
	 * @param unknown $userid
	 * @param unknown $evtid
	 * @return string
	 */
	public static function push($tocken, $app, $studioid, $userid, $evtid) {
		$studio = C\model::load('live.vs.studio')->get_one($studioid);
		$uri    = '';
		$where  = array('stype'=>1, 'status'=>1);
		if ($studio['iscasual']) {
			$where['stype'] = 2;
		}
		$list   = C\model::load('live.vs.rtmp')->get_only_column(0, -1, $where, 'rtmp');
		if ($list) {
			$idx  = C\config::$nowtime % count($list);
			if ($studio['iscasual']) {
				$uri  = sprintf(self::conn_uri, $list[$idx], $userid, $evtid);
			} else {
				$uri  = sprintf(self::push_uri, $list[$idx], $app, $tocken, $studioid, $userid, $evtid);
			}
			$field = array('studioid'=>$studioid, 'eventid'=>$evtid, 'origin'=>$app, 'url'=>$uri, 'stime'=>C\config::$nowtime);
			C\model::load('live.vs.stueventorigin')->new_one($field);
		}
		return $uri;
	}
	
	/**
	 * 获取推流地址资料信息
	 * @param array $studio
	 * @param int   $evtid
	 */
	public static function casual_recv($studio, $evtid) {
		$uri  = '';
		if (!$studio['rtmp']) {
			return $uri;
		}
		$data = parse_url($studio['rtmp']);
		$host = $data['host'].':'.$data['port'];
		$uri  = sprintf(self::conn_uri, $host, $studio['userid'], $evtid);
		return $uri;
	}
	
	/**
	 * 获取推流地址信息
	 * @param unknown $tocken
	 * @param unknown $studioid
	  * @param unknown $userid
	 * @param unknown $evtid
	 * @return string
	 */
	public static function recv($tocken, $studioid, $userid, $evtid) {
		$studio = C\model::load('live.vs.studio')->get_one($studioid);
		if ($studio && $studio['iscasual']) {
			return self::casual_recv($studio, $evtid);
		}
		$uri    = '';
		$where  = array('stype'=>0, 'status'=>1);
		$list   = C\model::load('live.vs.rtmp')->get_only_column(0, -1, $where, 'rtmp');
		if ($list) {
			$idx  = C\config::$nowtime % count($list);
			$uri  = sprintf(self::live_uri, $list[$idx],  $tocken, $studioid, $userid, $evtid);
		}
		return $uri;
	}
	
	/**
	 * 获取http imapi地址
	 * @return string
	 */
	public static function imapi() {
		$config = C\config::load('im', false);
		$idx    = C\config::$nowtime % count($config['api']);
		$api    = 'http://'.$config['api'][$idx];
		return $api;
	}
	
	/**
	 * 获取IM连接地址信息
	 * @return string
	 */
	public static function im() {
		$config = C\config::load('im', false);
		$idx    = C\config::$nowtime % count($config['im']);
		$imconf =  $config['im'][$idx];
		return $imconf;
	}
	
	/**
	 * 获取IM连接地址信息
	 * @return string
	 */
	public static function ims() {
		$maxlen = 3;
		$config = C\config::load('im', false);
		$len    = count($config['im']);
		if ($len < $maxlen) {
			return $config['im'];
		}
		$keys   = array_rand($config['im'], $maxlen);
		$list   = array();
		foreach ($keys as $idx) {
			$list[] = $config['im'][$idx];
		}
		return $list;
	}
}
